package com.ews.onlineexchange.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.Model;

import org.apache.commons.lang.time.DateUtils;

@Model
public class DateUtil {
	private static final String[] parsePatterns = new String[] {"yyyy.MM.dd", "yyyy-MM-dd","yyyy-M-dd","yyyy-M-d",
			"yyyy-MM-dd HH:mm:SS.ss", "yyyy年MM月dd日", "yyyy年M月dd日", "yyyy年MM月", "yyyy/M/d",
			"yyyy年M月", "yyyy/MM/dd", "yyyy/MM", "yyyy/M/dd", "yyyyMMdd",
			"yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss","MM/dd/yyyy"};

	public static Date getDate(String str) throws ParseException {
		return DateUtils.parseDate(str, parsePatterns);
	}
	
	// 本月(年 月 )
		public static Map<String, Integer> getTimesThisMonth() {

			Map<String, Integer> map = new HashMap<String, Integer>();
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH) + 1;
			
			map.put("year", year);
			map.put("month", month);
			
			return map;
		}
		
		// 下月（年 月）
		public static Map<String, Integer> getTimesNextMonth() {
			
			Map<String, Integer> map = new HashMap<String, Integer>();
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, 1);
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH) + 1;
			
			map.put("year", year);
			map.put("month", month);
			
			return map;
		}
		
		// 上月（年 月）
		public static Map<String, Integer> getTimesLastMonth() {
			
			Map<String, Integer> map = new HashMap<String, Integer>();
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -1);
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH) + 1;
			
			map.put("year", year);
			map.put("month", month);
			
			return map;
		}
		
		
		public static int getWeekNumOfNow() {
			Calendar cal = Calendar.getInstance();
			return cal.get( Calendar.WEEK_OF_MONTH );
		}
		

		// 获得本周一0点时间
		public static Date getTimesWeekMorning() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			return cal.getTime();
		}

		// 获得本周日24点时间
		public static Date getTimesWeekNight() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getTimesWeekMorning());
			cal.add(Calendar.DAY_OF_WEEK, 7);
			cal.add(Calendar.SECOND, -1);
			return cal.getTime();
		}

		// 获得本月第一天0点时间
		public static Date getTimesMonthMorning() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
			return cal.getTime();
		}

		// 获得本月最后一天24点时间
		public static Date getTimesMonthNight() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.HOUR_OF_DAY, 24);
			cal.add(Calendar.SECOND, -1);
			return cal.getTime();
		}

		// 获得本周一
		public static Date getThisWeekMonday() {
			Calendar cal = Calendar.getInstance();
			while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
				cal.add(Calendar.DAY_OF_WEEK, -1);
			}
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			return cal.getTime();
		}
		
		// 获得本周日
		public static Date getThisWeekSunday() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getThisWeekMonday());
			cal.add(Calendar.DATE, 6);
			return cal.getTime();
		}
		
		// 获得本周五
		public static Date getThisWeekToFriday() {
			Calendar cal = Calendar.getInstance();
			while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
				cal.add(Calendar.DAY_OF_WEEK, -1);
			}
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			return cal.getTime();
		}

		// 获得本周六 七点45
		public static Date getThisWeekSaturday() {
			Calendar cal = Calendar.getInstance();
			// 每周从周一开始
			while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
				cal.add(Calendar.DAY_OF_WEEK, -1);
			}
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 7, 45, 0);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			return cal.getTime();
		}
		
		// 获得上周一
		public static Date getLastWeekFromMonday() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getThisWeekMonday());
			cal.add(Calendar.DATE, -7);
			return cal.getTime();
		}

		// 获得上周五
		public static Date getLastWeekToFriday() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getThisWeekToFriday());
			cal.add(Calendar.DATE, -7);
			return cal.getTime();
		}
		
		// 获得下周一
		public static Date getNextWeekFromMonday() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getThisWeekMonday());
			cal.add(Calendar.DATE, 7);
			return cal.getTime();
		}

		// 获得下周五
		public static Date getNextWeekToFriday() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getThisWeekToFriday());
			cal.add(Calendar.DATE, 7);
			return cal.getTime();
		}

		public static String getStrByDate(Date date, String pattern) {
			if (date == null)
				return "";
			SimpleDateFormat sf = new SimpleDateFormat(pattern);
			return sf.format(date);
		}

		public static Date getDateByStr(String str, String pattern) {
			if (str == null)
				return null;
			Date parseDate = null;
			SimpleDateFormat sf = new SimpleDateFormat(pattern);
			try {
				parseDate = sf.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return parseDate;
		}

		public static String autoFixMonth(Integer month) {
			if (month < 10)
				return "0" + month;
			else
				return month.toString();
		}

		public static Map<String, Integer> getYearThisMonthMap() {
			Calendar cal = Calendar.getInstance();
			Map<String, Integer> map = new HashMap<>();
			map.put("year", cal.get(Calendar.YEAR));
			map.put("month", cal.get(Calendar.MONTH) + 1);
			return map;
		}

		public static Map<String, Integer> getYearLastMonthMap() {
			Calendar cal = Calendar.getInstance();
			Map<String, Integer> map = new HashMap<>();
			Integer month = cal.get(Calendar.MONTH);
			Integer year = cal.get(Calendar.YEAR);
			if (month == 0) {
				month = 12;
				year = year - 1;
			}
			map.put("year", year);
			map.put("month", month);
			return map;
		}

		public static Date[] getWeekBeginAndEndDate(Integer weekOffset) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.WEEK_OF_MONTH, weekOffset);
			cal.set(Calendar.DAY_OF_WEEK, 2);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			Date beginDate = cal.getTime();
			
			cal.set(Calendar.DAY_OF_WEEK, 7);
			cal.add(Calendar.SECOND, -1);
			Date endDate = cal.getTime();
			return new Date[] {beginDate, endDate};
		}
		
		public static Integer[] getWeekReportQueryWeek() {
			Calendar cal = Calendar.getInstance();
			if (cal.get(Calendar.DAY_OF_WEEK) < 6) {
				cal.add(Calendar.WEEK_OF_MONTH, -1);
			} else if (cal.get(Calendar.DAY_OF_WEEK) == 6) {
				if (cal.get(Calendar.HOUR_OF_DAY) <= 12) {
					cal.add(Calendar.WEEK_OF_MONTH, -1);
				}
			}
			return new Integer[] {cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.WEEK_OF_MONTH)};
		}
		
		public static Integer[] getWeekReportEditWeek () {
			Calendar cal = Calendar.getInstance();
			return new Integer[] {cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.WEEK_OF_MONTH)};
		}
		
		// 获得某年第一天0点时间
		public static Date getFirstDayOfYear(Integer year) {
			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
//			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
			return cal.getTime();
		}

		// 获得某年最后一天23点59时间
		public static Date getLastDayOfYear(Integer year) {
			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.set(Calendar.YEAR, year);
			cal.roll(Calendar.DAY_OF_YEAR, - 1);
			cal.set(Calendar.HOUR_OF_DAY, 24);
			cal.add(Calendar.SECOND, -1);
			return cal.getTime();
		}
		
		public static List<Integer> getTwelveMonths(){
			List<Integer> months = new ArrayList<Integer>();
			months.add(1);
			months.add(2);
			months.add(3);
			months.add(4);
			months.add(5);
			months.add(6);
			months.add(7);
			months.add(8);
			months.add(9);
			months.add(10);
			months.add(11);
			months.add(12);
			return months;
		}
		
		public static void main(String[] args) {
			System.out.println(getFirstDayOfYear(2019));
			System.out.println(getLastDayOfYear(2019));
			
		}
}
